function bci=bciTrainSVM(bci,T)

% train svm: right vs left
TRAIN = T.dat;
idx1=T.label == bci.eventsToClassify(1);
idx2=T.label == bci.eventsToClassify(2);
idx = find(T.label ~= bci.eventsToClassify(1) & T.label~=bci.eventsToClassify(2));

LABELS=zeros(size(T.label));
LABELS(idx1) = 1;
LABELS(idx2) = -1; % only first two classes
% discard all other events train samples 
LABELS(idx,:)=[];
TRAIN(idx,:) = [];

% train the classifier
d = data(TRAIN,LABELS);
bci.movdirSVM = svm;
bci.movdirSVM.optimizer='libsvm';
if isempty(bci.param.movdirC),
    bci.movdirSVM.C = getDefC(TRAIN);
elseif ~isnumeric(bci.param.movdirC),
    bci.movdirSVM.C = getDefC(TRAIN)*str2double(bci.param.movdirC);
else
    bci.movdirSVM.C = bci.param.movdirC;
end
bci.movdirSVM.algorithm.verbosity=0;
[tr bci.movdirSVM] = train(bci.movdirSVM,d);
%fprintf('N1: %i, N2: %i\n',sum(LABELS>0),sum(LABELS<0));

bci.movdirError = 1-sum(tr.X==tr.Y)/length(tr.X);

% %%----------------
% function f=getReducedW(W,perc)
%     if isnan(W),
%         error('Weight vector is NaN. Check regularization parameter C.');
%     end
%     [Wsort sortIdx] = sort(W);
%     thresh = sum(W)*perc;
%     threshIdx = find(cumsum(Wsort)<thresh);
%     if isempty(threshIdx),
%         f=true(size(W));
%         return
%     end
%     wTh = W(sortIdx(threshIdx(end)));
%     f = W>wTh;
